﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV2.Data.Model;
using WebApiV2.Domain;
using WebApiV2.Domain.Enum;
using WebApiV2.Interface.Log;

namespace WebApiV2.Implement.Log
{
    public class Log : ILog
    {
        private readonly IRespository<Logs> _logsRespository;

        public Log(IRespository<Logs> logsRespository)
        {
            _logsRespository = logsRespository;
        }
        public void Clear()
        {
            var list = _logsRespository.Table.ToList();
            _logsRespository.Deleted(list);

        }

        public void Delete(Logs log)
        {
            _logsRespository.Deleted(log);

        }

        public Logs GetLogById(int id)
        {
            return _logsRespository.GetbyId(id);
        }

        public void Insert(Logs log)
        {
            _logsRespository.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogsLever logLevel = LogsLever.错误, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;

            var list = _logsRespository.Table.Where(x => x.CreateTime >= bTime && x.CreateTime <= eTime && x.LogLevel == (int)logLevel);

            if (userId != 0)
            {
                list = list.Where(x => x.UserId == userId);

            }
            return list;
        }
    }
}
